package com.swordget.controller;

import com.swordget.entity.vo.BaseDataMappingTypeVo;
import com.swordget.entity.vo.BaseDataMappingVo;
import com.swordget.entity.vo.SimplePageResponseVo;
import com.swordget.entity.vo.SimpleResponseVo;
import com.swordget.service.impl.BaseDataMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @Author: David
 * @Date: Created in 9:32 2021/5/8
 * @Description: 基础数据映射Controller
 * @Modified By:
 * @Version:
 */
@Slf4j
@RestController
@RequestMapping("/baseData")
public class BaseDataMappingController {

    @Autowired
    private BaseDataMappingService service;

    @GetMapping
    public SimplePageResponseVo<Object> getDataType() {
        List<BaseDataMappingTypeVo> dataList = service.getDataType();
        return SimplePageResponseVo.builder()
                .total(dataList.size())
                .rows(dataList)
                .build();
    }

    @GetMapping("/data/{type}")
    public List<BaseDataMappingVo> getData(@PathVariable String type) {
        List<BaseDataMappingVo> dataList = service.getData(type);
        return dataList;
    }

    @PostMapping("/import")
    public SimpleResponseVo importData(MultipartHttpServletRequest request) {
        try {
            String typeName = request.getParameter("typeName");
            MultipartFile file = request.getFile("baseFile");
            boolean f = service.importData(typeName, file);
        } catch (Exception e) {
            log.error("", e);
            return SimpleResponseVo.fail(e.getMessage());
        }
        return SimpleResponseVo.success();
    }

    @GetMapping("/baseUrlList")
    public List<BaseDataMappingVo> baseUrlList() {
        List<BaseDataMappingVo> dataList = null;
        try {
            dataList = service.baseUrlList();
        } catch (UnsupportedEncodingException e) {
            log.error("", e);
        }
        return dataList;
    }



}
